
namespace AdminSG3L.Domain.Entities;

/// <summary>
/// 菜单实体
/// </summary>
public class AppMenu : EntityBase
{
    /// <summary>
    /// 菜单名称，必填
    /// </summary>
    public string MenuName { get; private set; }
    /// <summary>
    /// 菜单编码，可选
    /// </summary>
    public string? MenuCode { get; private set; }
    /// <summary>
    /// 拥有该菜单的角色集合
    /// </summary>
    public ICollection<AppRole> Roles { get; private set; } = [];
    /// <summary>
    /// 父级菜单Id，可选
    /// </summary>
    public Guid? ParentId { get; private set; }
    /// <summary>
    /// 父级菜单
    /// </summary>
    public AppMenu? Parent { get; private set; }
    /// <summary>
    /// 子菜单集合
    /// </summary>
    public ICollection<AppMenu> Children { get; private set; } = [];
    /// <summary>
    /// 图标，可选
    /// </summary>
    public string? Icon { get; private set; }
    /// <summary>
    /// 路径，可选
    /// </summary>
    public string? Path { get; private set; }

    /// <summary>
    /// 私有构造函数，仅限工厂方法调用
    /// </summary>
    private AppMenu(string menuName, string? menuCode, Guid? parentId, string? icon, string? path, string? description)
    {
        MenuName = menuName;
        MenuCode = menuCode;
        ParentId = parentId;
        Icon = icon;
        Path = path;
        Description = description;
    }

    /// <summary>
    /// 创建AppMenu对象的工厂方法
    /// </summary>
    /// <param name="menuName">菜单名称</param>
    /// <param name="menuCode">菜单编码</param>
    /// <param name="parentId">父级菜单ID</param>
    /// <param name="icon">图标</param>
    /// <param name="path">路径</param>
    /// <param name="description">描述</param>
    /// <returns>AppMenu对象</returns>
    public static AppMenu Create(string menuName, string? menuCode = null, Guid? parentId = null, ICollection<AppMenu>? children = null, ICollection<AppRole>? roles = null, string? icon = null, string? path = null, string? description = null, bool isSystem = false)
    {
        var menu = new AppMenu(menuName, menuCode, parentId, icon, path, description);
        menu.IsSystem = isSystem;
        return menu;
    }

    /// <summary>
    /// 添加子菜单
    /// </summary>
    public void AddChild(AppMenu child)
    {
        if (!Children.Contains(child))
        {
            Children.Add(child);
            child.SetParent(this);
        }
    }

    /// <summary>
    /// 移除子菜单
    /// </summary>
    public void RemoveChild(AppMenu child)
    {
        Children.Remove(child);
        child.ClearParent();
    }

    /// <summary>
    /// 设置父菜单
    /// </summary>
    public void SetParent(AppMenu parent)
    {
        Parent = parent;
        ParentId = parent.Id;
    }

    /// <summary>
    /// 清除父菜单
    /// </summary>
    public void ClearParent()
    {
        Parent = null;
        ParentId = null;
    }

    public void Update(string menuName, string? menuCode, Guid? parentId, string? icon, string? path, string? description)
    {
        if (IsSystem)
        {
            throw new InvalidOperationException("系统菜单不允许修改");
        }
        MenuName = menuName;
        MenuCode = menuCode;
        ParentId = parentId;
        Icon = icon;
        Path = path;
        Description = description;
    }
}
